//
//  ViewController.m
//  youbao
//
//  Created by 毕文 on 2018/8/1.
//  Copyright © 2018年 毕文. All rights reserved.
//

#import "ViewController.h"

#import <WebKit/WebKit.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <JavaScriptCore/JavaScriptCore.h>


#define IOS11 @available(iOS 11.0, *)
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<WKScriptMessageHandler,WKUIDelegate,WKNavigationDelegate> {
    
    WKWebViewConfiguration * _config;
}
@property (nonatomic , strong) WKWebView *webView;
@property (nonatomic ,strong)WKUserContentController * userCC;



@end

@implementation ViewController


- (WKWebView *)webView {
    if (!_webView) {
        
//        http://active.yybcoin.net
//        http://www.yybcoin.pro
        _config = [[WKWebViewConfiguration alloc]init];
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:_config];
        _webView.opaque = NO;
        _config.preferences.minimumFontSize = 10;
        _config.preferences.javaScriptEnabled = YES;
        _webView.allowsBackForwardNavigationGestures = YES;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://active.yybcoin.net"]]];
        [_webView sizeToFit];
        if (@available(iOS 11.0, *)) {
            
            _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    return _webView;
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
//    UIWebView *webView1 = [[UIWebView alloc]initWithFrame:self.view.bounds];
//    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
//    [self.view addSubview:webView1];
//    [webView1 loadRequest:request];
    
   
    
    
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    //这个回调里面， message.name代表方法名（‘本例为 callFunction’）， message.body代表JS给我们传过来的参数
    // NSLog(@"H5传过来的参数:%@ /n %@",message.name,message.body);
   
}
//
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [_webView evaluateJavaScript:@"ocInvokeJSMethod()"
               completionHandler:^(id _Nullable obj, NSError * _Nullable
                                   error) {
                   
                   NSLog(@".......%@",obj);
                   
               }];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    NSLog(@"加载完成");
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    
}
//最后VC销毁的时候一定要把handler移除
-(void)dealloc
{
    
    [_config.userContentController removeAllUserScripts];
}


@end
